package Server.Model.Base;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import Server.Agent.GameConfig;
import Server.Model.Hall.dthall;
import club.ClubMgr;
import common.routine.CommonDef;
import net.sf.json.JSONObject;
import redis.protocal.DbInfoMgr;
import redis.protocal.DwcRedisRecord;
import redis.protocal.UserRedisInfo;
import robot.GoldMgr;
import robot.RobotMgr;

class ServerUserPack{
	public ServerUserPack(int id,int android){
		this.dwUserID = id;
		this.isAndroidUser = android;
	}
	public int dwUserID;
	public int isAndroidUser;
	public String nickName;
}
class DianKongRecord{
	//多次点控的时候根据这个数据做操作
	public DianKongRecord(){
		m_winNum = 0;
		m_loseNum = 0;
		m_winLoseNum = 0;
	}
	public int m_winNum;
	public int m_loseNum;
	public int m_winLoseNum;
}
class DianKongDef{
	public int m_use;
	public int m_uid;
	public double m_currWonRate;
	public int m_dianKongNum;
	public int m_dianKongMode;
	public double m_rmbSum;
	public double m_chargeSum;
	public double m_diff;
	public double m_changeSum;
	public double m_change2Sum;
	public int m_type;//==1 要赢，==2 要输,==3赢100输100，
}
public abstract class IRoom{
	private int m_createTime;
	protected int m_clubId;
	private int m_clubWanFaId;
	public String m_wanFaStr;
	public boolean m_needRobot = false;
	public int m_robotStrategy;
	public boolean m_allowStranger;
	public int m_clubOwnerId;
	public int m_userRmbType;
	public int m_difen;
	public int m_ruchang;
	public int m_lichang;
	public double m_doubleDiFen;
	public double m_doubleRuChang;
	public double m_doubleLiChang;
	public String m_roomType = "ZUJU";//zuju,gold,dwc
	public int m_addRobotDelay = 0;
	public double m_eatNormalScore = 0;//玩家之间的输赢只记录record，只统计系统吃的税收和机器人输赢的
	public double m_eatRobotScore = 0;
	public double m_robotWinScore = 0;
	public boolean m_isDestoryed = false;
	public double m_giveBackAgentRate = 0.0;
	public double m_giveBackInvitorRate = 0.0;
	public String m_kouShuiMode = "";
	public int m_addRobotMinTime = 0;
	public int m_addRobotMaxTime = 0;
	public boolean m_roomStarted = false;
	public double m_qunKongRate = 0.0;
	public double m_qunKongMinTh = 0.0;
	public double m_qunKongMaxTh = 0.0;
	public int m_autoGameIapDelay = 10;
	public int m_autoReadyDelay = 1;
	public int m_goldKouYing = 0;
	public double m_goldKouYingRate = 0.0;
	public int m_dwcKouYing = 0;
	public double m_dwcKouYingRate = 0.0;
	public double m_goldKouYingFanliRate = 0.0;
	public double m_dwcKouYingFanliRate = 0.0;
	public List<DianKongDef> m_dianKongInfo = new ArrayList<DianKongDef>();
	public Map<Integer,DianKongRecord> m_dianKongNumMap= new HashMap<Integer,DianKongRecord>();
	public IRoom(){
		m_allowStranger = true;//为false则不显示列表内
		m_robotStrategy = 0;
		m_clubId = 0;
		m_needRobot = false;
		m_createTime = (int) (System.currentTimeMillis() / 1000);
		m_wanFaStr = "";
		m_clubOwnerId = 0;
		m_giveBackAgentRate = GoldMgr.getInstance().getCostGiveBackAgent();
		m_giveBackInvitorRate = GoldMgr.getInstance().getCostGiveBackInvitor();
		m_kouShuiMode = GoldMgr.getInstance().getKouShuiMode();
		m_addRobotMinTime = GoldMgr.getInstance().getAddRobotMinTime();
		m_addRobotMaxTime = GoldMgr.getInstance().getAddRobotMaxTime();
		m_qunKongRate = GoldMgr.getInstance().getQunKongRate();
		GoldMgr.QunKongParamDef param = GoldMgr.getInstance().getQunKongParam();
		m_qunKongMinTh = param.paramThresholdMin;
		m_qunKongMaxTh = param.paramThresholdMax;
		m_goldKouYing = GoldMgr.getInstance().getGoldKouYing();
		m_goldKouYingRate = GoldMgr.getInstance().getGoldKouYingRate();
		m_dwcKouYing = GoldMgr.getInstance().getDwcKouYing();
		m_dwcKouYingRate = GoldMgr.getInstance().getDwcKouYingRate();
		m_goldKouYingFanliRate = GoldMgr.getInstance().getGoldKouYingFanliRate();
		m_dwcKouYingFanliRate = GoldMgr.getInstance().getDwcKouYingFanliRate();
	}
	public boolean hasAutoDianKong(){
		if(m_clubOwnerId != 0)
			return true;
		for(int i = 0;i < m_dianKongInfo.size();i++){
			if(m_dianKongInfo.get(i).m_use == 1){
				return true;
			}
		}
		return false;
	}
	public void onUserQuitForDianKong(int uid){
		for(int i = 0;i < m_dianKongInfo.size();i++){
			if(m_dianKongInfo.get(i).m_uid == uid){
				m_dianKongInfo.remove(i);
				return;
			}
		}
	}
	public int getDianKongUid(){
		for(int i = 0;i < m_dianKongInfo.size();i++){
			DianKongDef def = m_dianKongInfo.get(i);
			if(def.m_use == 1){
				return def.m_uid;
			}
		}
		return 0;
	}
	public boolean inHall(){
		return m_clubOwnerId == 0;
	}
	public boolean inUserClub(){
		return m_clubOwnerId != 0;
	}
	public void checkDianKong(double score){
		for(int i = 0;i < m_dianKongInfo.size();i++){
			DianKongDef def = m_dianKongInfo.get(i);
			if(def.m_use == 1){
				if(def.m_type == 1){
					if(score >= def.m_changeSum){
						//点控结束
						def.m_use = 0;
						def.m_type = 0;
						break;
					}
				}else if(def.m_type == 2){
					if(-score > def.m_changeSum){
						//点控结束
						def.m_use = 0;
						def.m_type = 0;
						break;
					}
				}else if(def.m_type == 3){
					//先赢100再输100
					if(score > def.m_changeSum){
						//开始转向
						def.m_type = 4;
					}
				}else if(def.m_type == 4){
					if(score < def.m_change2Sum){
						//点控结束
						def.m_use = 0;
						def.m_type = 0;
					}
				}
			}
			return;
		}
	}
	public void clearDianKong(){
		m_dianKongInfo.clear();
	}
	public void clearDianKongDiff(){
		for(int i = 0;i < m_dianKongInfo.size();i++){
			m_dianKongInfo.get(i).m_diff = 0.0;
		}
	}
	public void requestDianKong(ServerUserPack[] userPack){
		//清除全部的
		//用户俱乐部不进行点控
		if(m_clubOwnerId != 0)
			return;
		m_dianKongInfo.clear();
		//检查是否有真人
		int realCnt = 0;
		for(int i = 0;i < userPack.length;i++){
			ServerUserPack user = userPack[i];
			if(user.dwUserID > 0 && user.isAndroidUser == 0){
				realCnt++;
			}
		}
		if(realCnt == 0)
			return;
		for(int i = 0;i < userPack.length;i++){
			ServerUserPack user = userPack[i];
			if(user.dwUserID > 0 && user.isAndroidUser == 0){
				SHMUser shmUser = dthall.getInstance().getUserInfo(user.dwUserID);
				UserRedisInfo redisInfo = dthall.getInstance().getSafeUserRedisInfo(user.dwUserID);
				DianKongDef def = new DianKongDef();
				def.m_uid = user.dwUserID;
				def.m_use = 0;
				def.m_currWonRate = shmUser.getUserRmbWonRate();
				def.m_dianKongNum = redisInfo.json.optInt("dianKongNum");
				def.m_rmbSum = redisInfo.json.optDouble("Rmb1_1") + redisInfo.getUserTiXianRmb1v1() + redisInfo.getBundFromSafeBox();
				def.m_chargeSum = redisInfo.json.optDouble("chargeSum");
				def.m_diff = 0.0;
				def.m_type = 0;
				m_dianKongInfo.add(def);
			}
		}
		//开始挑选点控对象
		//有wonrate小于门槛的优先，其次挑选资金/充值最小的那个，没有就挑选点控次数最少的
		int dianKongIndex = -1;
		int index = -1;
		int cnt = 0;
		//先判断是否存在系统设置的
		for(int i = 0;i < m_dianKongInfo.size();i++){
			DianKongDef def = m_dianKongInfo.get(i);
			JSONObject json = GoldMgr.getInstance().getDianKongSet(def.m_uid);
			if(json == null)
				continue;
			int type = json.optInt("type");
			if(type == 1 || type == 2 ||type == 3){
				def.m_type = type;
				def.m_changeSum = json.optInt("value1");
				def.m_change2Sum = json.optInt("value2");
				dianKongIndex = i;//找到第一个
				break;
			}else{
				continue;
			}
		}
		if(dianKongIndex != -1){
			GoldMgr.getInstance().clearDianKongSet(m_dianKongInfo.get(index).m_uid);
		}
		for(int i = 0;i < m_dianKongInfo.size();i++){
			DianKongDef def = m_dianKongInfo.get(i);
			if(def.m_currWonRate < m_qunKongMinTh){
				def.m_diff = Math.abs(def.m_currWonRate - 1.0);
				def.m_type = 2;
				cnt++;
			}else if(def.m_currWonRate > m_qunKongMaxTh){
				def.m_diff = Math.abs(def.m_currWonRate - 1.0);
				def.m_type = 1;
				cnt++;
			}
		}
		if(dianKongIndex == -1 && cnt > 0){
			//可以找到点控用户
			double diff = 0.1;
			for(int i = 0;i < m_dianKongInfo.size();i++){
				DianKongDef def = m_dianKongInfo.get(i);
				if(def.m_diff > diff){
					diff = def.m_diff;
					index = i;
				}
			}
			if(index != -1){
				dianKongIndex = index;
			}
		}
		if(dianKongIndex == -1){
			clearDianKongDiff();
			//资金充值比率
			cnt = 0;
			for(int i = 0;i < m_dianKongInfo.size();i++){
				DianKongDef def = m_dianKongInfo.get(i);
				double rate = def.m_rmbSum / def.m_chargeSum;
				if(rate > 2.0){
					def.m_diff = rate - 2.0;
					cnt++;
					def.m_type = 2;
				}else if(rate < 0.2){
					def.m_diff = 2.0 - rate;
					cnt++;
					def.m_type = 1;
				}
			}
			if(cnt > 0){
				double diff = 0.1;
				for(int i = 0;i < m_dianKongInfo.size();i++){
					DianKongDef def = m_dianKongInfo.get(i);
					if(def.m_diff > diff){
						diff = def.m_diff;
						index = i;
						def.m_type = 1;
					}
				}
				if(index != -1){
					dianKongIndex = index;
				}
			}
		}
		if(dianKongIndex == -1){
			//从点控最小找
			int dianCnt = 10000;
			for(int i = 0;i < m_dianKongInfo.size();i++){
				DianKongDef def = m_dianKongInfo.get(i);
				if(def.m_dianKongNum < dianCnt){
					dianCnt = def.m_dianKongNum;
					index = i;
					if(def.m_rmbSum > 1000){
						def.m_type = 2;
					}else if(def.m_rmbSum < 300){
						def.m_type = 1;
					}else{
						def.m_type = 3;
					}
					def.m_type = 1;
				}
			}
			if(index != -1){
				dianKongIndex = index;
			}
		}
		if(dianKongIndex != -1 && m_dianKongInfo.get(dianKongIndex).m_type != 0){
			DianKongDef def = m_dianKongInfo.get(dianKongIndex);
			//找到对象了
			UserRedisInfo redisInfo = dthall.getInstance().getSafeUserRedisInfo(def.m_uid);
			redisInfo.json.put("dianKongNum", redisInfo.json.optInt("dianKongNum") + 1);
			if(def.m_type == 1){
				//要赢,根据资金量计算出来要赢多少
				def.m_use = 1;
				def.m_changeSum = CommonDef.randInt(100, 150);
				if(def.m_rmbSum > 1000){
					def.m_changeSum = CommonDef.randInt(150, 250);
				}
				if(m_doubleDiFen < 1.0){
					def.m_changeSum *= m_doubleDiFen;
				}else if(m_doubleDiFen == 1.0){
					
				}else if(m_doubleDiFen > 1.0){
					def.m_changeSum *= 2;
				}
				//比例控制，当前用户真金比例控制
				def.m_changeSum = GoldMgr.getInstance().getDianKongCurrRmbRateLimit(def.m_rmbSum,def.m_changeSum);
				//最大控制 
				def.m_changeSum = GoldMgr.getInstance().getDianKongWinMaxLimit(def.m_changeSum);
				//一部分几率随机到先赢后输
				if(CommonDef.getRandomInt(100) < GoldMgr.getInstance().getDianKongWin2LoseRate()){
					def.m_type = 3;//随机到先赢后输
					def.m_change2Sum = -def.m_changeSum * 1.5;
				}else if(m_dianKongNumMap.containsKey(def.m_uid)){
					DianKongRecord record = m_dianKongNumMap.get(def.m_uid);
					if(record.m_winNum >= record.m_winLoseNum){
						def.m_type = 3;//随机到先赢后输
						def.m_change2Sum = -def.m_changeSum * 1.5;	
					}
				}
			}else if(def.m_type == 2){
				//要输，根据资金量计算出来要输多少
				def.m_use = 1;
				def.m_changeSum = CommonDef.randInt(100, 150);
				if(def.m_rmbSum > 1000){
					def.m_changeSum = CommonDef.randInt(150, 250);
				}
			}else if(def.m_type == 3){
				def.m_use = 1;
				def.m_changeSum = CommonDef.randInt(100, 150);
				if(def.m_rmbSum > 1000){
					def.m_changeSum = CommonDef.randInt(150, 250);
				}
				if(m_doubleDiFen < 1.0){
					def.m_changeSum *= m_doubleDiFen;
				}else if(m_doubleDiFen == 1.0){
					
				}else if(m_doubleDiFen > 1.0){
					def.m_changeSum *= 2;
				}if(CommonDef.getRandomInt(100) < GoldMgr.getInstance().getDianKongWin2LoseRate()){
					def.m_type = 3;//随机到先赢后输
					def.m_change2Sum = -def.m_changeSum * 1.5;
				}else if(m_dianKongNumMap.containsKey(def.m_uid)){
					DianKongRecord record = m_dianKongNumMap.get(def.m_uid);
					if(record.m_winNum >= record.m_winLoseNum){
						def.m_type = 3;//随机到先赢后输
						def.m_change2Sum = -def.m_changeSum * 1.5;	
					}
				}
			}
			//记录点控信息
			if(m_dianKongNumMap.containsKey(def.m_uid)){
				if(def.m_type == 1){
					m_dianKongNumMap.get(def.m_uid).m_winNum++;
				}else if(def.m_type == 2){
					m_dianKongNumMap.get(def.m_uid).m_loseNum++;
				}else if(def.m_type == 3){
					m_dianKongNumMap.get(def.m_uid).m_winLoseNum++;
				}
			}else{
				DianKongRecord record = new DianKongRecord();
				if(def.m_type == 1)
					record.m_winNum = 1;
				else if(def.m_type == 2)
					record.m_loseNum = 1;
				else
					record.m_winLoseNum = 1;
				m_dianKongNumMap.put(def.m_uid, record);
			}
		}
	}
	public double getDianKongRate(int uid){
		for(int i = 0;i < m_dianKongInfo.size();i++){
			DianKongDef def = m_dianKongInfo.get(i);
			if(def.m_use == 1 && def.m_uid == uid){
				if(def.m_type == 1){
					return 1.99;
				}else if(def.m_type == 2){
					return 0.0;
				}else if(def.m_type == 3){
					return 1.99;
				}else if(def.m_type == 4){
					return 0.0;
				}
			}
		}
		SHMUser user = dthall.getInstance().getUserInfo(uid);
		return user.getUserRmbWonRate();
	}
	public void setRoomStarted(boolean start){
		m_roomStarted = start;
	}
	public void addEatNormalScore(double score){
		m_eatNormalScore += score;
	}
	public void addEatRobotScore(double score){
		m_eatRobotScore += score;
	}
	public void addRobotWinScore(double score){
		m_robotWinScore += score;
	}
	public void handleGoldGiveBack(int uid,double cost){
		//只有agent
		if(m_giveBackAgentRate != 0){
			DbInfoMgr.getInstance().giveBackGoldForUserAgent(uid,(int)(cost * m_giveBackAgentRate));
		}
	}
	public void handleRmbGiveBack(int uid,double cost){
		//有agent,暂时不用
		//DbInfoMgr.getInstance().giveBackRmbForUserAgentAndInvitor(uid,cost * m_giveBackAgentRate,cost * m_giveBackInvitorRate);
	}
	public ServerUserPack[] cloneServerPdkUserPack(Server.Poker.PDK.ServerUserItem[] users){
		ServerUserPack[] rev = new ServerUserPack[users.length];
		for(int i = 0;i < users.length;i++){
			ServerUserPack item = new ServerUserPack(users[i].dwUserID,users[i].isAndroidUser);
			item.nickName = users[i].szNickName;
			rev[i] = item;
		}
		return rev;
	}
	public ServerUserPack[] cloneServerDdzUserPack(Server.Poker.DDZ.ServerUserItem[] users){
		ServerUserPack[] rev = new ServerUserPack[users.length];
		for(int i = 0;i < users.length;i++){
			ServerUserPack item = new ServerUserPack(users[i].dwUserID,users[i].isAndroidUser);
			item.nickName = users[i].szNickName;
			rev[i] = item;
		}
		return rev;
	}
	public ServerUserPack[] cloneServerUserPack(IServerUserItem[] users){
		ServerUserPack[] rev = new ServerUserPack[users.length];
		for(int i = 0;i < users.length;i++){
			ServerUserPack item = new ServerUserPack(users[i].dwUserID,users[i].isAndroidUser);
			item.nickName = users[i].szNickName;
			rev[i] = item;
		}
		return rev;
	}
	public void beforeDestroy(ServerUserPack[] users,int [] intscore,double []doublescore){
		if(m_isDestoryed){
			return;
		}
		m_isDestoryed = true;
		if(isGoldType()){
			double applysum = 0;
			double leftsum = 0;
			for(int i = 0;i < users.length;i++){
				if(users[i].dwUserID > 0 && users[i].isAndroidUser == 2){
					//只处理机器人，没有掉线问题
					RobotMgr.getInstance().FreeRobot(users[i].dwUserID);
					SHMUser user = dthall.getInstance().getUserInfo(users[i].dwUserID);
					applysum += user.getApplyNum();
					leftsum += user.getCoin(0);
				}
			}
			GoldMgr mgr = GoldMgr.getInstance();
			if(m_eatNormalScore > 0){
				//吃的正常用户的税收
				mgr.eatUserGold((int)m_eatNormalScore);
			}
			if(m_eatRobotScore > 0){
				//吃的机器人的税收
				mgr.giveBackRobotGold((int)m_eatRobotScore);
			}
			if(m_robotWinScore != 0){
				//机器人输赢
				mgr.addRobotWinGold((int)m_robotWinScore);
				//机器人输赢了,可能是机器人的也可能是真人的,统一加紧系统吃进
				mgr.eatUserGold((int)m_robotWinScore);				
			}
			//机器人剩余的返还系统奖池
			if(applysum >= (leftsum + m_eatRobotScore)){
				//机器人有损耗只返回left部分
				mgr.giveBackRobotGold((int)leftsum);
			}else{
				//机器人有赢只返回申请的
				mgr.giveBackRobotGold((int)(applysum - m_eatRobotScore));
			}
		}else if(isDwcType()){
			double applysum = 0;
			double leftsum = 0;
			for(int i = 0;i < users.length;i++){
				if(users[i].dwUserID > 0 && users[i].isAndroidUser == 2){					
					RobotMgr.getInstance().FreeRobot(users[i].dwUserID);
					SHMUser user = dthall.getInstance().getUserInfo(users[i].dwUserID);
					applysum += user.getApplyNum();
					leftsum += user.getRmb1v1Value();
				}
			}

			BigDecimal b1 = new BigDecimal(applysum);
			applysum = b1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); 
			BigDecimal b2 = new BigDecimal(leftsum);
			leftsum = b2.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); 
			GoldMgr mgr = GoldMgr.getInstance();
			if(m_eatNormalScore > 0){
				//吃的正常用户的税收
				mgr.eatUserRmb(m_eatNormalScore);
			}
			if(m_eatRobotScore > 0){
				//吃的机器人的税收
				mgr.giveBackRobotRmb(m_eatRobotScore);
			}
			if(m_robotWinScore != 0){
				BigDecimal b3 = new BigDecimal(m_robotWinScore);
				m_robotWinScore = b3.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
				//机器人输赢了,可能是机器人的也可能是真人的,统一加紧系统吃进
				mgr.eatUserRmb(m_robotWinScore);				
			}
			//机器人剩余的返还系统奖池
			if(applysum >= (leftsum + m_eatRobotScore)){
				//机器人有损耗只返回left部分
				mgr.giveBackRobotRmb(leftsum);
			}else{
				//机器人有赢只返回申请的
				mgr.giveBackRobotRmb(applysum - m_eatRobotScore);
			}
			//统计胜率与输赢
			for(int i = 0;i < doublescore.length;i++){
				double score = doublescore[i];
				if(score != 0 && users[i].dwUserID > 0){
					DwcRedisRecord.getInstance().addUserRankDwcWonMoney(users[i].dwUserID, users[i].nickName, score);
					DwcRedisRecord.getInstance().addUserRankDwcWonRate(users[i].dwUserID, users[i].nickName, score);
				}
			}
			//统计局数
			//只有一个人或者分数全为0就不记录了
			int canrecord = 0;
			for(int i = 0;i < doublescore.length;i++){
				double score = doublescore[i];
				if(score != 0){
					canrecord = 1;
					break;
				}
			}
			if(canrecord == 0){
				return;
			}
			String name = getGameName();
			JSONObject json = new JSONObject();
			json.put("difen", m_doubleDiFen);
			json.put("gameName", name);
			json.put("roomId", GetID());
			json.put("clubid",m_clubId);
			json.put("clubownerid",m_clubOwnerId);
			json.put("jushu",GetRoundInfo());
			json.put("time", UserRedisInfo.getRecordTimeKey());
			json.put("comTime", (int)(System.currentTimeMillis() / 1000));
			for(int i = 0;i < doublescore.length;i++){
				double score = doublescore[i];
				if(users[i].dwUserID > 0){
					if(users[i].isAndroidUser == 2 && m_clubOwnerId == 0){
						json.put("name" + i,"机器:" + users[i].nickName);
					}else{
						json.put("name" + i, users[i].nickName);
					}
					json.put("score" + i, score);
					json.put("uid" + i, users[i].dwUserID);
				}
			}
			DwcRedisRecord.getInstance().addZhanJi(json);
		}else if(isFreeZuJuType()){
			int canrecord = 0;
			for(int i = 0;i < doublescore.length;i++){
				double score = doublescore[i];
				if(score != 0){
					canrecord = 1;
					break;
				}
			}
			if(canrecord == 0){
				return;
			}
			//统计局数
			String name = getGameName();
			JSONObject json = new JSONObject();
			json.put("difen", "免费组局");
			json.put("gameName", name);
			json.put("roomId", GetID());
			json.put("clubid",m_clubId);
			json.put("clubownerid",m_clubOwnerId);
			json.put("jushu",GetRoundInfo());
			json.put("time", UserRedisInfo.getRecordTimeKey());
			json.put("comTime", (int)(System.currentTimeMillis() / 1000));
			for(int i = 0;i < doublescore.length;i++){
				double score = doublescore[i];
				if(users[i].dwUserID > 0){
					if(users[i].isAndroidUser == 2 && m_clubOwnerId == 0){
						json.put("name" + i,"机器:" + users[i].nickName);
					}else{
						json.put("name" + i, users[i].nickName);
					}
					json.put("score" + i, score);
					json.put("uid" + i, users[i].dwUserID);
				}
			}
			DwcRedisRecord.getInstance().addZhanJi(json);
		}
	}
	public void setRoomType(String type){
		m_roomType = type;
		if(m_roomType.equals("GOLD") || m_roomType.equals("DWC")){
			setNeedRobot(true,GoldMgr.getInstance().getRobotStrategy());
			genAddRobotDelay();
		}
	}
	public void genAddRobotDelay(){
		if(m_addRobotMaxTime != 0){
			m_addRobotDelay = CommonDef.randInt(m_addRobotMinTime,m_addRobotMaxTime);
			return;
		}
		int maxplayer = GetMaxPlayerCount();
		int currplayer = getPlayerCount();
		if(maxplayer == currplayer){
			setNeedRobot(false,0);
			return;
		}
		int maxtime = (maxplayer - currplayer) * 4;
		int mintime = (int) ((maxplayer - currplayer) * 1.5);
		if(maxtime > mintime)
			m_addRobotDelay = CommonDef.randInt(mintime,maxtime);
		else
			m_addRobotDelay = CommonDef.randInt(maxtime,mintime);
	}
	public boolean isZuJuType(){
		return m_roomType.equalsIgnoreCase("ZUJU") || m_roomType.equalsIgnoreCase("FREEZUJU");
	}
	public boolean isGoldType(){
		return m_roomType.equalsIgnoreCase("GOLD");
	}
	public boolean isDwcType(){
		return m_roomType.equalsIgnoreCase("DWC");
	}
	public boolean isFreeZuJuType(){
		return m_roomType.equalsIgnoreCase("FREEZUJU");
	}
	public String getRoomType(){
		return m_roomType;
	}
	public double getShuiShou(String type){
		return GoldMgr.getInstance().getKouShuiShuiLv();
	}
	public int getCreateTime(){
		return m_createTime;
	}
	public void setBeiShu(double difen,double ruchang,double lichang){
		m_difen = (int)difen;
		m_ruchang = (int)ruchang;
		m_lichang = (int)lichang;
		m_doubleDiFen = difen;
		m_doubleRuChang = ruchang;
		m_doubleLiChang = lichang;
	}
	public int getDifen(){ return m_difen;}
	public int getRuChang(){ return m_ruchang;}
	public int getLiChang(){ return m_lichang;}
	public double getDoubleDifen(){ return m_doubleDiFen;}
	public double getDoubleRuChang(){ return m_doubleRuChang;}
	public double getDoubleLiChang(){ return m_doubleLiChang;}
	
	public void setAllowStranger(boolean allow){
		m_allowStranger = allow;
	}
	public boolean allowStranger(){
		return m_allowStranger;
	}
	public void setNeedRobot(boolean need,int strategy){
		m_needRobot = need;
		m_robotStrategy = strategy;
	}
	public boolean getNeedRobot(){
		return m_needRobot;
	}
	public int getRobotStrategy(){
		return m_robotStrategy;
	}
	public void setInClub(int id){
		m_clubId = id;
	}
	public int getClubId(){
		return m_clubId;
	}
	public void setClubOwnerId(int id){
		m_clubOwnerId = id;
	}
	public int getClubOwnerId(){
		return m_clubOwnerId;
	}
	public void setClubWanFa(int id){
		m_clubWanFaId = id;
	}
	public int getClubWanFaId(){
		return m_clubWanFaId;
	}
	public void setClubWanFaStr(String wanfa){
		m_wanFaStr = wanfa;
	}
	public String getClubWanFaStr(){
		return m_wanFaStr;
	}
	public String getGameName(){
		String type = GetType();
		if(type.equals("DDZ"))
			return "斗地主";
		else if(type.equals("PDK"))
			return "跑得快";
		else if(type.equals("TTZ"))
			return "推筒子";
		else if(type.equals("NIUNIU"))
			return "5人牛牛";
		else if(type.equals("ZJH"))
			return "扎金花";
		else if(type.equals("BJ") || type.equals("LABJ"))
			return "比鸡";
		else if(type.equals("SSZ"))
			return "十三水";
		else if(type.equals("FJSSZ"))
			return "十三水";
		else if(type.equals("SSZLIU"))
			return "十三水";
		else if(type.equals("NIUNIU10"))
			return "10人牛牛";
		else
			return "麻将类";
	}
	public 	int GetMaxPlayerCount(){
		String type = GetType();
		if(type.equals("DDZ"))
			return 3;
		else if(type.equals("PDK"))
			return 3;
		else if(type.equals("TTZ"))
			return 5;
		else if(type.equals("NIUNIU"))
			return 5;
		else if(type.equals("ZJH"))
			return 5;
		else if(type.equals("BJ") || type.equals("LABJ"))
			return 5;
		else if(type.equals("SSZ"))
			return 4;
		else if(type.equals("FJSSZ"))
			return 5;
		else if(type.equals("SSZLIU"))
			return 6;
		else
			return 4;
	}
	public void OnRoundInfoChange(){
		String roundInfo = GetRoundInfo();
		if(m_clubId != 0){
			ClubMgr.getInstance().m_clubHandler.handleOn_ClubRoomRoundChange(m_clubId,GetID(),roundInfo);
		}
		if(roundInfo.startsWith("0")){
			
		}else{
			m_needRobot = false;
			m_roomStarted = true;
		}
	}
	public void addRobotUser(){
		//大厅的房间才处理自动解散
		if(!m_roomStarted && (m_roomType.equals("GOLD") || m_roomType.equalsIgnoreCase("DWC"))){
			int currTime = (int) (System.currentTimeMillis() / 1000);
			if(currTime - m_createTime > 60 * 4){
				DoDismissRoomByOverTime();
			}else if(getPlayerCount() == 0){
				//空房间不处理
				m_createTime = currTime;
			}
		}
		if(m_needRobot == false)
			return;
		if(m_addRobotDelay > 0){
			m_addRobotDelay--;
			return;
		}
		if(getPlayerCount()  == 0){
			genAddRobotDelay();
			return;
		}
		if(m_clubOwnerId != 0){
			return;
		}
		if(m_roomType.equals("GOLD")){
			if(m_difen > GoldMgr.getInstance().getRobotGoldDiFenLimit()){
				setNeedRobot(false,0);
			}
		}else if(m_roomType.equals("DWC")){
			if(m_doubleDiFen > GoldMgr.getInstance().getRobotRmbDiFenLimit()){
				setNeedRobot(false,0);
			}
		}
		DoAddRobotPlayer();
		genAddRobotDelay();
	}
	public boolean canUserJoinRentRoom(int upAgentId){
		//往上代理只要有一个是，即可加入
		if(upAgentId == 1000 && m_clubOwnerId != 0){
			return true;
		}
		if(upAgentId == m_clubOwnerId)
			return true;
		while(upAgentId != 0){
			if(upAgentId == 1000){
				return false;
			}
			UserRedisInfo info = dthall.getInstance().getSafeUserRedisInfo(upAgentId);
			if(info.json.optInt("upAgentUid") == m_clubOwnerId)
				return true;
			upAgentId = info.json.optInt("upAgentUid");
			if(upAgentId == 1000){
				return false;
			}
		}
		return false;
	}
	abstract  public	int 		GetID();
	abstract  public	String		GetRoundInfo();
	abstract  public	String 		GetType();
	abstract  public	int 		GetCreator();
	abstract  public	void 		OnClientClose(int uid);
	abstract  public	int 		CanSit(int _Uid);
	abstract public	int    			FindSeat(int _Uid);
	abstract public	void    		OnUserSit(int _Uid);
	abstract public	void    		OnUserStandUp(int _Uid,boolean leaveroom);
	abstract public 	void 		OnDeal(int dt);
	abstract public  int			getDealTime();
	abstract public  String 		getRoomInfo();
	abstract public  String 		getRoomDesc();
	abstract public  int			getPlayerCount();
	abstract public  int			getCostGem();
	abstract public  int			getCellScore();
	abstract public  int			getMinScore();
	abstract public  int			getStatus();
	abstract public  String 		getPayway();
	
	public void DoAddRobotPlayer(){};
	public void DoDismissRoomByOverTime(){};
};
